package com.webbei.Backstage.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.webbei.Backstage.service.BackstateSer;
import com.webbei.custom.service.CustomSer;
import com.webbei.pojo.PicInfor;
import com.webbei.product.action.Product;
import com.webbei.util.Thumbnail;


public class ShowAllProducts {
	private CustomSer cs;
	private BackstateSer bao;
	private List<Product> products;
	public String execute(){
		System.out.println("showAllProducts....");
		products=bao.getAllProduct();
	
		for(int i=0;i<products.size();i++){
			String bgPic = products.get(i).getUrl();
			String auto = products.get(i).getAutoLogos();
			if(auto!=null){
				JSONObject json = JSONObject.fromObject(products.get(i).getAutoLogos());
				int left = (Integer) json.get("left");
				int top = (Integer)json.get("top");
				int height = (Integer)json.get("height");
				int width = (Integer)json.get("width");
				System.out.println("left....."+left);
				List<PicInfor> pics = new ArrayList<PicInfor>();  
				PicInfor pic = new PicInfor();
				pic.setPicUrl("img/7A21411631.png");
				pic.setLeft(left);
				pic.setTop(top);
				pic.setHeight(height);
				pic.setWidth(width);
				pics.add(pic);
				String fromImg = ServletActionContext.getServletContext().getRealPath("/");
				System.out.println("fromImg:"+fromImg);
				String url;
				try {
					url = Thumbnail.saveImageAsJpg(fromImg+cs.drawPic(bgPic, pics), 250);
					products.get(i).setNewUrl(url);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			
		}
		System.out.println(products);
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setHeader("Pragma","No-cache");
		response.setHeader("Cache-Control","no-cache"); 
		response.setDateHeader("Expires", 0);
		return "success";
	}
	public BackstateSer getBao() {
		return bao;
	}
	public void setBao(BackstateSer bao) {
		this.bao = bao;
	}
	public List<Product> getProducts() {
		return products;
	}
	public void setProducts(List<Product> products) {
		this.products = products;
	}
	public void setCs(CustomSer cs) {
		this.cs = cs;
	}


}
